package com.globalwave.pos.inventory.service;

import java.sql.Timestamp;
import java.util.Date;

import com.globalwave.common.U;
import com.globalwave.common.exception.BusinessException;
import com.globalwave.pos.inventory.entity.InventoryPeriod;

import static com.globalwave.pos.inventory.entity.InventoryPeriod.* ;

/**
 * Period Status:Not-Open/Open/Close
 * @author Administrator
 *
 */
public class InventoryPeriodBO extends InventoryBaseBO {


	public InventoryPeriod current() throws Exception {
		//(InventoryPeriod)accessor.findName("inventory:retrieveCurrentInventoryPeriod", null) ;
		InventoryPeriod currentPeriod = getOpenPeriod();
		
		if (currentPeriod == null) {/*
			Date dateNow = PosFunc.getCurrentDate(accessor) ;
			currentPeriod = new InventoryPeriodPO() ;
			currentPeriod.setPeriodId(dateNow.getYear() * 100 + dateNow.getMonth()) ;
			currentPeriod.setStartDate(new Date(currentPeriod.getEndDate().getTime() + 1)) ;
			currentPeriod.setEndDate(DateFunc.getLastDayOfLastMonth(DateFunc.addMonth(currentPeriod.getEndDate(), 2))) ;
			currentPeriod.setOpenDateTime(dateNow) ;
			currentPeriod.setEndDate(null) ;
			currentPeriod.setPeriodStatus(INVENTORY_PERIOD_STATUS_OPEN) ;
	    	
	    	accessor.insert(currentPeriod) ;
			return currentPeriod ;*/
			throw new BusinessException("err.inv.no.current.period") ;
		} else {
			return currentPeriod ;
		}
	}

	public InventoryPeriod next(InventoryPeriod currentPeriod) throws Exception {

		return (InventoryPeriod)jdbcDao.findName("inventory:retrieveNextInventoryPeriod", currentPeriod, InventoryPeriod.class) ;
	}
	
	public InventoryPeriod open(InventoryPeriod currentPeriod) throws Exception {

		Timestamp dateNow = U.currentTimestamp() ;
		
	    InventoryPeriod nextPeriod = next(currentPeriod) ;
	    
	    if(nextPeriod == null) {
	    	nextPeriod = (InventoryPeriod)currentPeriod.clone() ;
	    	int nextPeriodId = (int)(currentPeriod.getDate_to()/100000000) ;
	    	nextPeriod.setId(nextPeriodId) ;
	    	nextPeriod.setName_("auto" + nextPeriodId) ;
	    	
	    	nextPeriod.setDate_from(currentPeriod.getDate_to());

	    	Date dateFrom = U.int2Date(currentPeriod.getDate_from()) ;
	    	dateFrom = U.addMonths(dateFrom, 1) ;
	    	nextPeriod.setDate_to(U.date2int(dateFrom)) ;
	    	
	    	nextPeriod.setOpen_on(dateNow) ;
	    	nextPeriod.setClose_on(null) ;
	    	nextPeriod.setStatus_(INVENTORY_PERIOD_STATUS_OPEN) ;
	    	
	    	jdbcDao.insert(nextPeriod) ;
	    } else {
	    	nextPeriod.setOpen_on(dateNow) ;
	    	nextPeriod.setStatus_(INVENTORY_PERIOD_STATUS_OPEN) ;
	    	jdbcDao.update(nextPeriod) ;
	    }
	    
	    return nextPeriod ;
	}
	
	public void close(InventoryPeriod currentPeriod) throws Exception {
		
		Timestamp dateNow = U.currentTimestamp() ;
		currentPeriod.setClose_on(dateNow) ;
		currentPeriod.setStatus_(INVENTORY_PERIOD_STATUS_CLOSE) ;
		
		jdbcDao.update(currentPeriod) ;
	    
	}
	public InventoryPeriod closeAndOpenNext(InventoryPeriod currentPeriod) throws Exception {
		close(currentPeriod) ;
		return open(currentPeriod) ;
	}
	
	public InventoryPeriod getOpenPeriod() throws Exception {
		InventoryPeriod period = new InventoryPeriod() ;
		period.setStatus_(INVENTORY_PERIOD_STATUS_OPEN) ;
		return (InventoryPeriod)jdbcDao.find(period) ;
	}
}
